package monsterRun.server.model.entities;

import java.sql.*;

import javax.security.auth.login.LoginException;

import monsterRun.data.DataConnection;
import monsterRun.server.model.events.AddPlayerException;

public class PlayerInfo {
	/** Instance variables */
	private String firstname;
	private String surname;
	private String address; 
	private int phone;
	private String username;
	private String password;
	private int score;

	public PlayerInfo(){}

	/**
	 * 	Every Player Class will always have 1 PlayerInfo associated with it.
	 * 	1 to 1 Relationship 
	 * 
	 * Constructor
	 * 
	 * @param firstname
	 *            the first name of the player
	 * @param surname
	 *            the surname of the player
	 * @param address
	 *            the player's address (home)
	 * @param phone
	 *            the phone number of the player
	 * @param username PRIMARY KEY
	 *            the unique identifier of the player
	 * @param password
	 *            the password of the player
	 *            
	 *   @author s3252820 Gareth Ng 2012
	 */
	public PlayerInfo(String firstname, String surname, String address, 
			int phone, String username, String password){
		this.firstname = firstname;
		this.surname = surname;
		this.address = address;
		this.phone = phone;
		this.username = username;
		this.password = password;
		this.score = 0;
	}

	/**
	 * Constructor
	 * 
	 * @param username
	 *            the unique identifier of the player
	 * @param password
	 *            the password of the player
	 */
	public PlayerInfo(String username, String password) throws SQLException{
		DataConnection cn = new DataConnection();

		// Search the database for the user if password matches
		String query = "SELECT * FROM Player WHERE Username = '" + username + 
		"' AND Pass = '" + password + "'";

		ResultSet rs = cn.runQuery(query);

		if (rs.next()){
			this.firstname = rs.getString(1);
			this.surname = rs.getString(2);
			this.address = rs.getString(3);
			this.phone = Integer.parseInt(rs.getString(4));
			this.username = rs.getString(5);
			this.password = rs.getString(6);
			this.score = Integer.parseInt(rs.getString(7));
			cn.close();
		}
		else {
			cn.close();
			System.out.println("Invalid credentials, try again");
		}
	}
	/**
	 * Method 
	 * 
	 * Add Player Information to the Database Connection
	 */

	public boolean login(String username, String password){
		try {
			DataConnection cn = new DataConnection();
			if(!cn.login(username, password)){
				throw new LoginException("Cannot login in to DB");
			} else{
				cn = new DataConnection();
				String query = "SELECT * FROM Player WHERE Username = '" + username + 
				"' AND Pass = '" + password + "'";

				ResultSet rs = cn.runQuery(query);

				rs.next();
				this.firstname = rs.getString(1);
				this.surname = rs.getString(2);
				this.address = rs.getString(3);
				this.phone = Integer.parseInt(rs.getString(4));
				this.username = rs.getString(5);
				this.password = rs.getString(6);
				this.score = Integer.parseInt(rs.getString(7));
			}
			cn.close();
			return true;
		}
		catch (LoginException lge){
			System.out.println("Error logging into the DB");
			return false;
		}
		catch (Exception e){
			System.out.println("Error, please revise");
			e.printStackTrace();
			return false;
		}
	}

	public boolean addPlayerInfo(String firstname, String surname, String address, 
			int phone, String username, String password) {	
		try {
			DataConnection cn = new DataConnection();
			if(!cn.addPlayerInfo(firstname, surname, address, phone, username, password)){
				cn.close();
				return false;
			} else {
				cn.close();
				return true;
			}
		}
		catch (Exception e){
			System.out.println("Error, please revise");
			return false;
		}
	}

	/**	Getters
	 */

	public String getFirstname() {
		return firstname;
	}

	public String getSurname() {
		return surname;
	}

	public String getAddress() {
		return address;
	}

	public int getPhone() {
		return phone;
	}

	public String getUsername() {
		return username;
	}

	public String getPassword() {
		return password;
	}

	public int getScore() {
		return score;
	}


	/** Add Score
	 */
	public void addScore(){
		this.score++;
	}

}
